<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="代码" prop="code">
      <el-input v-model="dataForm.code" placeholder="代码"></el-input>
    </el-form-item>
    <el-form-item label="名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="名称"></el-input>
    </el-form-item>
    <el-form-item label="规格" prop="product">
      <el-input v-model="dataForm.product" placeholder="规格"></el-input>
    </el-form-item>
    <el-form-item label="规格" prop="batch">
      <el-input v-model="dataForm.batch" placeholder="规格"></el-input>
    </el-form-item>
    <el-form-item label="单位" prop="lot">
      <el-input v-model="dataForm.lot" placeholder="单位"></el-input>
    </el-form-item>
    <el-form-item label="数据来源" prop="fromDb">
      <el-input v-model="dataForm.fromDb" placeholder="数据来源"></el-input>
    </el-form-item>
    <el-form-item label="原始数量" prop="countOriginal">
      <el-input v-model="dataForm.countOriginal" placeholder="原始数量"></el-input>
    </el-form-item>
    <el-form-item label="单位转换后的数量" prop="countNow">
      <el-input v-model="dataForm.countNow" placeholder="单位转换后的数量"></el-input>
    </el-form-item>
<!--    <el-form-item label="更新时间" prop="updateTime">-->
<!--      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>-->
<!--    </el-form-item>-->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          product: '',
          batch: '',
          lot: '',
          fromDb: '',
          countOriginal: '',
          countNow: '',
          updateTime: ''
        },
        dataRule: {
          code: [
            { required: true, message: '代码不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          product: [
            { required: true, message: '规格不能为空', trigger: 'blur' }
          ],
          batch: [
            { required: true, message: '规格不能为空', trigger: 'blur' }
          ],
          lot: [
            { required: true, message: '单位不能为空', trigger: 'blur' }
          ],
          fromDb: [
            { required: true, message: '数据来源不能为空', trigger: 'blur' }
          ],
          countOriginal: [
            { required: true, message: '原始数量不能为空', trigger: 'blur' }
          ],
          countNow: [
            { required: true, message: '单位转换后的数量不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`wms/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.wmsData.code
                this.dataForm.name = data.wmsData.name
                this.dataForm.product = data.wmsData.product
                this.dataForm.batch = data.wmsData.batch
                this.dataForm.lot = data.wmsData.lot
                this.dataForm.fromDb = data.wmsData.fromDb
                this.dataForm.countOriginal = data.wmsData.countOriginal
                this.dataForm.countNow = data.wmsData.countNow
                this.dataForm.updateTime = data.wmsData.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`wms/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'product': this.dataForm.product,
                'batch': this.dataForm.batch,
                'lot': this.dataForm.lot,
                'fromDb': this.dataForm.fromDb,
                'countOriginal': this.dataForm.countOriginal,
                'countNow': this.dataForm.countNow,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
